package com.whz.iplocation.ipregion.search.db;

import lombok.Data;

/**
 * ip定位接口分析器
 * <a href="https://blog.dnomd343.top/qqwry.dat-analyse/">cw file analyzer</a>
 *
 * @author whz
 */
@Data
public class CwFileAnalyzer {

    private long secret;

    public CwFileAnalyzer(long firstSecret) {
        this.secret = firstSecret;
    }

    private long nextSecret() {
        this.secret = (((secret * 2053) + 1) & 0xFF);
        return this.secret;
    }

    public byte[] decode(byte[] data) {
        if (data.length < 512) {
            throw new RuntimeException("file size must > 512 bytes.");
        }
        for (int i = 0; i < 512; i++) {
            data[i] = (byte) (nextSecret() ^ data[i]);
        }
        return data;
    }
}